package assembler;

import java.util.HashMap;
import java.util.Map;

public class OpCodeMap {
	public static final int WORD = -1;
	public static final int BYTE = -2;	
	public static final int RESW = -3;
	public static final int RESB = -4;
	public static final int notFound = -5;
	public static final int COMM = -6;
	public static final int BASE = -7;
	public static Map<String, Integer> opCodeTable;
	public static Map<String, Integer> opCodeFormat;

	public static void setOpCodeTable(){
		opCodeTable = new HashMap<String, Integer>();
		opCodeTable.put("ADDR",  0x90);	
		opCodeTable.put("COMPR", 0xA0);
		opCodeTable.put("SUBR",  0x94);		
		opCodeTable.put("ADD",   0x18);		
		opCodeTable.put("SUB",   0x1C);	
		opCodeTable.put("MUL",   0x20);	
		opCodeTable.put("DIV",   0x24);	
		opCodeTable.put("COMP",  0x28);	
		opCodeTable.put("J",     0x3C);
		opCodeTable.put("JEQ",   0x30);
		opCodeTable.put("JGT",   0x34);		
		opCodeTable.put("JLT",   0x38);		
		opCodeTable.put("JSUB",  0x48);	
		opCodeTable.put("LDA",   0x00);
		opCodeTable.put("LDB",   0x68);
		opCodeTable.put("LDCH",  0x50);		
		opCodeTable.put("LDL",   0x08);	
		opCodeTable.put("LDT",   0x74);		
		opCodeTable.put("LDX",   0x04);
		opCodeTable.put("RSUB",  0x4C);
		opCodeTable.put("TIX",   0x2C);
		opCodeTable.put("TIXR",  0xB8);				
		opCodeTable.put("RD",    0xD8);
		opCodeTable.put("TD",    0xE0);		
		opCodeTable.put("WD",    0xDC);
		opCodeTable.put("STA",   0x0C);
		opCodeTable.put("STB",   0x78);		
		opCodeTable.put("STCH",  0x54);
		opCodeTable.put("STL",   0x14);
		opCodeTable.put("STX",   0x10);
		opCodeTable.put("CLEAR", 0xB4);
		opCodeTable.put("WORD",  WORD);		
		opCodeTable.put("BYTE",  BYTE);
		opCodeTable.put("RESW",  RESW);		
		opCodeTable.put("RESB",  RESB);
		opCodeTable.put(".",  COMM);
		opCodeTable.put("BASE",  BASE);		
		
		opCodeFormat = new HashMap<String, Integer>();
		opCodeFormat.put("ADDR",  0x02);	
		opCodeFormat.put("COMPR", 0x02);
		opCodeFormat.put("SUBR",  0x02);		
		opCodeFormat.put("CLEAR", 0x02);		
		opCodeFormat.put("TIXR",  0x02);	
	}

	public static int getOpCode(String opCodeString){
		if(opCodeTable == null)
			setOpCodeTable();
		try	{
			if(opCodeTable.containsKey(opCodeString))
				return opCodeTable.get(opCodeString);
			else
				return notFound;
		}catch (NullPointerException e){
			return notFound;
		}
	}
	
	public static boolean isOpCodeInFormatTable(String opCodeString){
		if(opCodeFormat == null)
			setOpCodeTable();
		return (opCodeFormat.containsKey(opCodeString));
	}	

	public static boolean isOpCodeValid(String opCodeString)	{
		return getOpCode(opCodeString) >= 0 ? true:false;
	}
}